﻿// ---------------------------------------------------------------------------
const WINDOW_HEIGHT_BODY = 330;                                // 安装内容高度
const WINDOW_HEIGHT_FOOT = 150;                                // 安装操作高度
// ---------------------------------------------------------------------------
const WINDOW_WIDTH = 700;                                      // 窗口宽高
const WINDOW_HEIGHT = WINDOW_HEIGHT_BODY + WINDOW_HEIGHT_FOOT; // 窗口宽高
// ---------------------------------------------------------------------------
const TOOLBAR_HEIGHT = 40;                                     // 工具栏高度
const TOOLBAR_BUTTON_WIDTH = TOOLBAR_HEIGHT;                   // 窗口按钮宽度
const TOOLBAR_BUTTON_HEIGHT = TOOLBAR_HEIGHT;                  // 窗口按钮高度
// ---------------------------------------------------------------------------
const INSTALL_BUTTON_WIDTH = 200;                              // 安装按钮宽度
const INSTALL_BUTTON_HEIGHT = 50;                              // 安装按钮高度
// ---------------------------------------------------------------------------
const INSTALL_LOCATION_BUTTON_WIDTH = 480;                     // 安装位置宽度
const INSTALL_LOCATION_BUTTON_HEIGHT = 40;                     // 安装位置高度
// ---------------------------------------------------------------------------
const INSTALL_LOCATION_CHOOSE_BUTTON_WIDTH = 100;              // 选择位置按钮宽度
const INSTALL_LOCATION_CHOOSE_BUTTON_HEIGHT = 40;              // 选择位置按钮高度
// ---------------------------------------------------------------------------
const CHECK_LICENSE_BUTTON_WIDTH = 16;                         // 许可选中按钮宽度
const CHECK_LICENSE_BUTTON_HEIGHT = 16;                        // 许可选中按钮高度

var WindowBodyPanel: TPanel;           // 内容面板
var WindowFootPanel: TPanel;           // 操作面板
var MinButton: HWND;                   // 最小化按钮
var CloseButton: HWND;                 // 关闭按钮
var InstallButton: HWND;               // 安装按钮
var InstallLocationButton: HWND;       // 安装位置
var InstallLocationFont: TFont;        // 安装位置字体
var InstallLocationChooseButton: HWND; // 安装位置选择按钮
var CheckLicenseButton: HWND;          // 许可选中按钮
var CurrentDPI: Integer;               // 当前DPI
var IsInitialize: Boolean;             // 是否初始化

type TButtonEventProc = procedure(h: HWND);
type TPBProc = function(h: HWND; message, w_param, l_param: LongInt): LongInt;

function ImgLoad(h: hwnd; FileName: PAnsiChar; Left, Top, Width, Height: integer; Stretch, IsBkg: boolean): LongInt; external 'ImgLoad@files:botva2.dll STDCALL DelayLoad';
procedure ImgSetVisibility(Image: LongInt; Visible: boolean); external 'ImgSetVisibility@files:botva2.dll STDCALL DelayLoad';
procedure ImgApplyChange(h: hwnd); external 'ImgApplyChanges@files:botva2.dll STDCALL DelayLoad';
procedure ImgApplyChanges(h: hwnd); external 'ImgApplyChanges@files:botva2.dll STDCALL DelayLoad';
function ButtonCreate(hParent: hwnd; Left, Top, Width, Height: Integer; FileName: PAnsiChar; ShadowWidth: integer; IsCheckBtn: boolean): hwnd; external 'BtnCreate@files:botva2.dll STDCALL DelayLoad';
procedure ButtonSetText(h: HWND; Text: PAnsiChar);  external 'BtnSetText@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetFont(h: HWND; Font: Cardinal); external 'BtnSetFont@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetTextAlignment(h: HWND; HorIndent, VertIndent: integer; Alignment:DWORD); external 'BtnSetTextAlignment@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetEvent(h: HWND; EventID: Integer; Event: LongWord); external 'BtnSetEvent@files:botva2.dll STDCALL DelayLoad';
function  ButtonGetCheck(h: HWND): boolean; external 'BtnGetChecked@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetCheck(h: HWND; value: boolean); external 'BtnSetChecked@{tmp}\botva2.dll STDCALL DelayLoad';
procedure ButtonSetEnable(h: HWND; value: boolean); external 'BtnSetEnabled@{tmp}\botva2.dll STDCALL DelayLoad';

function ReleaseCapture(): LongInt; external 'ReleaseCapture@user32.dll STDCALL';
function WrapButtonCallback(Callback: TButtonEventProc; Count: integer) : LongWord; external 'wrapcallback@files:InnoCallback.dll STDCALL DelayLoad';

// 窗口拖动事件
procedure WizardMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
    ReleaseCapture();
    SendMessage(WizardForm.Handle, $0112, $F012, 0);
    Log('鼠标拖动')
end;

// 按钮事件
procedure ButtonEvent(h: hwnd; Callback: TButtonEventProc);
begin
    Log('按钮事件');
    ButtonSetEvent(h, 1, WrapButtonCallback(Callback, 1));
end;

// 触发安装事件
procedure event_trigger_install(button: HWND);
begin
    Log('触发安装事件');
    // WizardForm.NextButton.OnClick(WizardForm);
end;

// Checkbox 切换
procedure CheckboxClick(button: HWND);
begin
    if ButtonGetCheck(button) = true then
    begin
        Log('正选')
        ButtonSetCheck(button, True);
    end else
    begin
        Log('反选')
        ButtonSetCheck(button, False);
    end;
end;

// 提取临时资源
procedure ExtractStaticFile();
begin
    Log('提取临时资源开始...');
    ExtractTemporaryFile('body-bg.png');
    ExtractTemporaryFile('foot-bg.png');
    ExtractTemporaryFile('logo.png');
    ExtractTemporaryFile('check.png');
    ExtractTemporaryFile('location.png');
    ExtractTemporaryFile('button_min.png');
    ExtractTemporaryFile('button_close.png');
    ExtractTemporaryFile('button_install.png');
    ExtractTemporaryFile('choose-location.png');
    ExtractTemporaryFile('background_welcome.png');
    Log('提取临时资源完成...');
end;

function DPIScale(value: Integer): Integer;
begin
    Result := value * CurrentDPI / 1000;
end;

procedure Initialize();
begin
    if IsInitialize then begin
        Log('已经初始化');
    end else begin
        Log('Initialize...');
        // 提取静态资源
        ExtractStaticFile();
        // 获取当前DPI
        CurrentDPI :=  1000 * WizardForm.Font.PixelsPerInch / 96;
        //
        WizardForm.InnerNotebook.Hide();
        WizardForm.OuterNotebook.Hide();
        WizardForm.Bevel.Hide();
        WizardForm.BorderStyle := bsNone;
        WizardForm.Position := poScreenCenter;
        WizardForm.Width := DPIScale(WINDOW_WIDTH);
        WizardForm.Height := DPIScale(WINDOW_HEIGHT);
        WizardForm.InnerPage.Enabled := False;
        WizardForm.ReadyPage.Enabled := False;
        WizardForm.WelcomePage.Enabled := False;
        WizardForm.LicensePage.Enabled := False;
        WizardForm.PasswordPage.Enabled := False;
        WizardForm.FinishedPage.Enabled := False;
        WizardForm.UserInfoPage.Enabled := False;
        WizardForm.InfoAfterPage.Enabled := False;
        WizardForm.SelectDirPage.Enabled := False;
        WizardForm.PreparingPage.Enabled := False;
        WizardForm.InstallingPage.Enabled := False;
        WizardForm.InfoBeforePage.Enabled := False;
        WizardForm.SelectTasksPage.Enabled := False;
        WizardForm.SelectComponentsPage.Enabled := False;
        WizardForm.SelectProgramGroupPage.Enabled := False;
    end;
end;

procedure CreateWelcome();
begin
    Initialize();

    // 内容面板
    WindowBodyPanel := TPanel.Create(WizardForm);
    WindowBodyPanel.Parent := WizardForm;
    WindowBodyPanel.ParentBackground := True;
    WindowBodyPanel.Color := clNone;
    WindowBodyPanel.Top := 0;
    WindowBodyPanel.Left := 0;
    WindowBodyPanel.Width := DPIScale(WINDOW_WIDTH);
    WindowBodyPanel.Height := DPIScale(WINDOW_HEIGHT_BODY);
    WindowBodyPanel.BorderWidth := 0;
    WindowBodyPanel.BevelKind := bkNone;
    WindowBodyPanel.BevelInner := bvNone;
    WindowBodyPanel.BevelOuter := bvNone;
    WindowBodyPanel.Visible := True;
    ImgLoad(WindowBodyPanel.Handle, ExpandConstant('{tmp}\body-bg.png'), 0, 0, DPIScale(WINDOW_WIDTH), DPIScale(WINDOW_HEIGHT_BODY), True, False);
    ImgApplyChanges(WindowBodyPanel.Handle);

    // 操作面板
    WindowFootPanel := TPanel.Create(WizardForm);
    WindowFootPanel.Parent := WizardForm;
    WindowFootPanel.ParentBackground := False;
    WindowFootPanel.Color := clNone;
    WindowFootPanel.Top := DPIScale(WINDOW_HEIGHT_BODY);
    WindowFootPanel.Left := 0;
    WindowFootPanel.Width := DPIScale(WINDOW_WIDTH);
    WindowFootPanel.Height := DPIScale(WINDOW_HEIGHT_FOOT);
    WindowFootPanel.BorderWidth := 0;
    WindowFootPanel.BevelKind := bkNone;
    WindowFootPanel.BevelInner := bvNone;
    WindowFootPanel.BevelOuter := bvNone;
    WindowFootPanel.Visible := True;
    ImgLoad(WindowFootPanel.Handle, ExpandConstant('{tmp}\foot-bg.png'), 0, 0, DPIScale(WINDOW_WIDTH), DPIScale(WINDOW_HEIGHT_FOOT), True, False);
    ImgApplyChanges(WindowFootPanel.Handle);

    // 创建最小化和关闭按钮
    CloseButton := ButtonCreate(WindowBodyPanel.Handle, DPIScale(WINDOW_WIDTH) - DPIScale(TOOLBAR_BUTTON_WIDTH), 0, DPIScale(TOOLBAR_BUTTON_WIDTH), DPIScale(TOOLBAR_BUTTON_HEIGHT), ExpandConstant('{tmp}\button_close.png'), 0, False);
    MinButton := ButtonCreate(WindowBodyPanel.Handle, DPIScale(WINDOW_WIDTH) - (DPIScale(TOOLBAR_BUTTON_WIDTH) * 2), 0, DPIScale(TOOLBAR_BUTTON_WIDTH), DPIScale(TOOLBAR_BUTTON_HEIGHT), ExpandConstant('{tmp}\button_min.png'), 0, False);

    // 安装按钮
    InstallButton := ButtonCreate(WindowBodyPanel.Handle, (DPIScale(WINDOW_WIDTH) / 2) - (DPIScale(INSTALL_BUTTON_WIDTH) / 2), (DPIScale(WINDOW_HEIGHT_BODY) - DPIScale(INSTALL_BUTTON_HEIGHT) - DPIScale(50)), DPIScale(INSTALL_BUTTON_WIDTH), DPIScale(INSTALL_BUTTON_HEIGHT), ExpandConstant('{tmp}\button_install.png'), 0, False);
    ButtonEvent(InstallButton, @event_trigger_install);

    // 安装位置和选择位置
    InstallLocationButton := ButtonCreate(WindowFootPanel.Handle, 60, DPIScale(50), DPIScale(INSTALL_LOCATION_BUTTON_WIDTH), DPIScale(INSTALL_LOCATION_BUTTON_HEIGHT), ExpandConstant('{tmp}\location.png') , 0, False);
    ButtonSetText(InstallLocationButton, WizardForm.DirEdit.Text);
    InstallLocationFont := TFont.Create;
    with InstallLocationFont do begin
        Size := DPIScale(10);
    end;
    ButtonSetFont(InstallLocationButton, InstallLocationFont.Handle);
    ButtonSetTextAlignment(InstallLocationButton, DPIScale(INSTALL_LOCATION_BUTTON_HEIGHT / 4), DPIScale(INSTALL_LOCATION_BUTTON_HEIGHT / 4), 0);
    // 选择按钮
    InstallLocationChooseButton := ButtonCreate(WindowFootPanel.Handle, DPIScale(INSTALL_LOCATION_BUTTON_WIDTH + 60), DPIScale(50), DPIScale(INSTALL_LOCATION_CHOOSE_BUTTON_WIDTH), DPIScale(INSTALL_LOCATION_CHOOSE_BUTTON_HEIGHT), ExpandConstant('{tmp}\choose-location.png') ,0 ,False);

    // ------------------------------------勾选用户协议------------------------------------
    CheckLicenseButton := ButtonCreate(WindowFootPanel.Handle, 60, DPIScale(50 + 20 + INSTALL_LOCATION_BUTTON_HEIGHT), DPIScale(CHECK_LICENSE_BUTTON_WIDTH), DPIScale(CHECK_LICENSE_BUTTON_HEIGHT), ExpandConstant('{tmp}\check.png'), 0, True);
    ButtonEvent(CheckLicenseButton, @CheckboxClick);

end;
